---
description: "Reference for Nitric's Python library - Subscribe a handler to a topic and receive new events for processing."
---

# Python - topic.subscribe()

<Note>
  This is reference documentation for the Nitric Python SDK. To learn about
  Topics and Pub/Sub in Nitric start with the [Async Messaging
  docs](/messaging#topics).
</Note>

Subscribes to a topic.

```python
from nitric.resources import topic
from nitric.application import Nitric
from nitric.context import MessageContext

updates = topic("updates")

@updates.subscribe
async def updates_sub(ctx: MessageContext):
  print(ctx.req.data)

Nitric.run()
```

## Parameters

<Properties>
  <Property name="middleware" required type="Middleware">
    The middleware (code) to be triggered by the topic.
  </Property>
</Properties>

## Examples

### Subscribe to a topic

```python
from nitric.resources import topic
from nitric.application import Nitric
from nitric.context import MessageContext

updates = topic("updates")

@updates.subscribe
async def updates_sub(ctx: MessageContext):
  print(ctx.req.data)

Nitric.run()
```

## Notes

- A function may only subscribe to a topic once, if multiple subscribers are required, create them in different services.
- A function may subscribe to OR publish to a topic but not both. This is in place to stop services calling themselves infinitely.
